using Microsoft.Office.Interop.Excel;
using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.IO;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationOrderDetailedReports
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private object missing = Missing.Value;

		private string _startupPath = null;

		public ExcelOperationOrderDetailedReports(string startupPath)
		{
			this._startupPath = startupPath;
			if (this._startupPath.Substring(this._startupPath.Length - 1, 1) != "\\")
			{
				this._startupPath += "\\";
			}
		}

		private void OpenExcel(string templetFile)
		{
			if (templetFile == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			if (!File.Exists(templetFile))
			{
				throw new ArgumentException("你指定的参数：templetFile Excel模板文件不存在", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Open(templetFile, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeDataToExcel(string templetFile, System.Data.DataTable dtDetailedReports)
		{
			this.OpenExcel(templetFile);
			string defaultVersionOfOutType = AppSettings.GetDefaultVersionOfOutType("detailedReports");
			if (defaultVersionOfOutType == "1")
			{
				this.MakeDataToExcel_1(dtDetailedReports);
			}
		}

		private void MakeDataToExcel_1(System.Data.DataTable dtDetailedReports)
		{
			int num = 0;
			try
			{
				for (int i = 0; i < dtDetailedReports.Rows.Count; i++)
				{
					decimal d = Convert.ToDecimal(dtDetailedReports.Rows[i]["宽"]);
					decimal d2 = Convert.ToDecimal(dtDetailedReports.Rows[i]["高"]);
					int value = Convert.ToInt32(dtDetailedReports.Rows[i]["产品数量"]);
					decimal num2 = Convert.ToDecimal((d * d2 / 1000000m).ToString("F4")) * value;
					decimal num3 = Convert.ToDecimal(((d + d2) * 2m / 1000m).ToString("F4")) * value;
					this.workSheet.Cells[3 + num, 1]= dtDetailedReports.Rows[i]["公司名称"].ToString();
					this.workSheet.Cells[3 + num, 2]= dtDetailedReports.Rows[i]["产品类型"].ToString();
					this.workSheet.Cells[3 + num, 3]= dtDetailedReports.Rows[i]["产品编号"].ToString();
					this.workSheet.Cells[3 + num, 4]= d.ToString();
					this.workSheet.Cells[3 + num, 5]= d2.ToString();
					this.workSheet.Cells[3 + num, 7]= value.ToString();
					this.workSheet.Cells[3 + num, 8]= num3.ToString();
					this.workSheet.Cells[3 + num, 9]= num2.ToString();
					this.workSheet.Cells[3 + num, 10]= dtDetailedReports.Rows[i]["产品价格"].ToString();
					this.workSheet.Cells[2 + num, 11]= dtDetailedReports.Rows[i]["产品备注"].ToString();
					num++;
				}
				string text = ((Range)this.workSheet.Cells[3, 1]).Text.ToString();
				string text2 = ((Range)this.workSheet.Cells[3, 2]).Text.ToString();
				int num4 = 3;
				for (int j = 3; j <= dtDetailedReports.Rows.Count - 1 + 3; j++)
				{
					if (((Range)this.workSheet.Cells[j, 2]).Text.ToString() == text2 && ((Range)this.workSheet.Cells[j, 1]).Text.ToString() == text)
					{
						int num5 = j;
						if (num5 == dtDetailedReports.Rows.Count - 1 + 3)
						{
							Range range = this.workSheet.get_Range(this.workSheet.Cells[num4, 2], this.workSheet.Cells[num5, 2]);
							range.ClearContents();
							range.MergeCells=true;
							range.Value2=text2;
							range.WrapText=true;
						}
					}
					else
					{
						int num5 = j - 1;
						if (text2 != "·" && text2.IndexOf("★") < 0)
						{
							Range range = this.workSheet.get_Range(this.workSheet.Cells[num4, 2], this.workSheet.Cells[num5, 2]);
							range.ClearContents();
							range.MergeCells=true;
							range.Value2=text2;
							range.WrapText=true;
						}
						num4 = j;
						text = ((Range)this.workSheet.Cells[j, 1]).Text.ToString();
						text2 = ((Range)this.workSheet.Cells[j, 2]).Text.ToString();
					}
				}
				text = ((Range)this.workSheet.Cells[3, 1]).Text.ToString();
				int num6 = 3;
				for (int j = 3; j <= dtDetailedReports.Rows.Count - 1 + 3; j++)
				{
					if (((Range)this.workSheet.Cells[j, 1]).Text.ToString() == text)
					{
						int num7 = j;
						if (num7 == dtDetailedReports.Rows.Count - 1 + 3)
						{
							Range range = this.workSheet.get_Range(this.workSheet.Cells[num6, 1], this.workSheet.Cells[num7, 1]);
							range.ClearContents();
							range.MergeCells=true;
							range.Value2=text;
							range.WrapText=true;
						}
					}
					else
					{
						int num7 = j - 1;
						if (text != "·" && text.IndexOf("★") < 0)
						{
							Range range = this.workSheet.get_Range(this.workSheet.Cells[num6, 1], this.workSheet.Cells[num7, 1]);
							range.ClearContents();
							range.MergeCells=true;
							range.Value2=text;
							range.WrapText=true;
						}
						num6 = j;
						text = ((Range)this.workSheet.Cells[j, 1]).Text.ToString();
					}
				}
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
